package com.smarth.zhizulian.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.activity.AgentWebActivity;
import com.smarth.zhizulian.activity.WebViewActivity;
import com.smarth.zhizulian.bean.News;
import com.smarth.zhizulian.utils.GlideUtils;
import com.smarth.zhizulian.utils.ViewHolder;

import java.util.List;

/**
 * Created by 18865 on 2017/12/20.
 */

public class NewsAdapter extends BaseObjectListAdapter {
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;
    final int TYPE_3 = 2;

    public NewsAdapter(Context context, List<? extends Object> datas) {
        super(context, datas);
    }


    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        final News news = (News) mDatas.get(position);
        switch (type) {
            case TYPE_1:
                convertView = getConvertView(convertView, R.layout.item_newst1);
                TextView tv_title = ViewHolder.get(convertView, R.id.tv_title);
                TextView tv_money = ViewHolder.get(convertView, R.id.tv_money);
                TextView tv_readnum = ViewHolder.get(convertView, R.id.tv_readnum);
                ImageView iv_pic1 = ViewHolder.get(convertView, R.id.iv_pic1);
                ImageView iv_pic2 = ViewHolder.get(convertView, R.id.iv_pic2);
                ImageView iv_pic3 = ViewHolder.get(convertView, R.id.iv_pic3);
                ImageView iv_highprice = ViewHolder.get(convertView, R.id.iv_highprice);
                ImageView iv_share = ViewHolder.get(convertView, R.id.iv_share);
                tv_title.setText(news.getTitle());
                tv_money.setText("" + news.getCoin());
                tv_readnum.setText("" + news.getReadCount());
                List<String> imgUrls = news.getImages();
                if (imgUrls != null && imgUrls.size() >= 3) {
                    GlideUtils.loadNews(mContext, imgUrls.get(0), iv_pic1);
                    GlideUtils.loadNews(mContext, imgUrls.get(1), iv_pic2);
                    GlideUtils.loadNews(mContext, imgUrls.get(2), iv_pic3);
                }
                iv_highprice.setVisibility("高价".equals(news.getType()) ? View.VISIBLE : View.GONE);
                iv_share.setVisibility("高价".equals(news.getType()) ? View.GONE : View.VISIBLE);
                break;
            case TYPE_2:
                convertView = getConvertView(convertView, R.layout.item_newst2);
                TextView tv_title2 = ViewHolder.get(convertView, R.id.tv_title);
                TextView tv_money2 = ViewHolder.get(convertView, R.id.tv_money);
                TextView tv_readnum2 = ViewHolder.get(convertView, R.id.tv_readnum);
                ImageView iv_pic12 = ViewHolder.get(convertView, R.id.iv_pic1);
                ImageView iv_highprice2 = ViewHolder.get(convertView, R.id.iv_highprice);
                ImageView iv_share2 = ViewHolder.get(convertView, R.id.iv_share);
                tv_title2.setText(news.getTitle());
                tv_money2.setText("" + news.getCoin());
                tv_readnum2.setText("" + news.getReadCount());
                List<String> imgUrls2 = news.getImages();
                if (imgUrls2 != null && imgUrls2.size() >= 1) {
                    GlideUtils.loadNews(mContext, imgUrls2.get(0), iv_pic12);
                }
                iv_highprice2.setVisibility("高价".equals(news.getType()) ? View.VISIBLE : View.GONE);
                iv_share2.setVisibility("高价".equals(news.getType()) ? View.GONE : View.VISIBLE);
                break;
            case TYPE_3:
                convertView = getConvertView(convertView, R.layout.item_newst3);
                TextView tv_title3 = ViewHolder.get(convertView, R.id.tv_title);
                TextView tv_money3 = ViewHolder.get(convertView, R.id.tv_money);
                TextView tv_readnum3 = ViewHolder.get(convertView, R.id.tv_readnum);
                ImageView iv_pic13 = ViewHolder.get(convertView, R.id.iv_pic1);
                ImageView iv_highprice3 = ViewHolder.get(convertView, R.id.iv_highprice);
                ImageView iv_share3 = ViewHolder.get(convertView, R.id.iv_share);
                tv_title3.setText(news.getTitle());
                tv_money3.setText("" + news.getCoin());
                tv_readnum3.setText("" + news.getReadCount());
                List<String> imgUrls3 = news.getImages();
                if (imgUrls3 != null && imgUrls3.size() >= 1) {
                    GlideUtils.loadNews(mContext, imgUrls3.get(0), iv_pic13);
                }
                iv_highprice3.setVisibility("高价".equals(news.getType()) ? View.VISIBLE : View.GONE);
                iv_share3.setVisibility("高价".equals(news.getType()) ? View.GONE : View.VISIBLE);
                break;
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AgentWebActivity.openForArticle(mContext, "" + news.getRelationId());
//                WebViewActivity.openForArticle(mContext, "test", "" + news.getRelationId());
            }
        });
        return convertView;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public int getItemViewType(int position) {
        News news = (News) mDatas.get(position);
        switch (news.getTemplateType()) {
            case 1:
                //一张大图
                return TYPE_3;
            case 2:
                //一张小图
                return TYPE_2;
            case 3:
                //三张小图
                return TYPE_1;
            default:
                return TYPE_1;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

}
